﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace Devfw.OpenAuth
{
    /// <summary>
    /// 
    /// Provides an internal structure to sort the parameter
    /// </summary>
    public class Parameter
    {
        private string name = null;
        private string value = null;

        public Parameter( string name, string value )
            : this( name, value, ParameterType.Get, true )
        {
        }

        public Parameter( string name, string value, ParameterType type )
            : this( name, value, type, true )
        {
        }

        public Parameter( string name, string value, ParameterType type, bool needSignature )
            : this(name, value, type, needSignature, true)
        {
        }

        public Parameter(string name, string value, ParameterType type, bool needSignature , bool needEncodePostData)
        {
            this.name = name;
            this.value = value;
            Type = type;
            NeedSignature = needSignature;
            NeedEncodePostData = needEncodePostData;
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public string Value
        {
            get
            {
                return value;
            }
        }

        public ParameterType Type
        {
            get;
            set;
        }

        public bool NeedSignature
        {
            get;
            set;
        }

        private bool _needEncodePostData = true;
        public bool NeedEncodePostData
        {
            get
            {
                return _needEncodePostData;
            }
            set
            {
                _needEncodePostData = value;
            }
        }

        public static List<Parameter> Parse( string query )
        {
            List<Parameter> result = new List<Parameter>();
            var nvc = System.Web.HttpUtility.ParseQueryString( query );

            return Parse( nvc );
        }

        public static List<Parameter> Parse( NameValueCollection nvc )
        {
            List<Parameter> result = new List<Parameter>();

            foreach( string key in nvc.Keys )
            {
                result.Add( new Parameter( key, nvc[key] ) );
            }

            return result;
        }

        public static List<Parameter> Combin( List<Parameter> list1, List<Parameter> list2 )
        {
            List<Parameter> list = new List<Parameter>();

            list.AddRange( list1 );
            list.AddRange( list2 );
            
            return list;
        }

        public static NameValueCollection ToNameValueCollection( List<Parameter> list )
        {
            NameValueCollection nvc = new NameValueCollection();
            foreach( Parameter p in list )
            {
                nvc.Add( p.Name, p.Value );
            }
            return nvc;
        }
    }

    public class PostParameter : Parameter
    {
        public PostParameter( string name, string value )
            : this( name, value, false )
        {
        }

        public PostParameter( string name, string value, bool isBinary )
            : this( name, value, isBinary, "", !isBinary )
        {
        }

        public PostParameter( string name, string value, bool isBinary, string extAtContentDisposition )
            : this( name, value, isBinary, extAtContentDisposition, !isBinary )
        {
        }

        public PostParameter( string name, string value, bool isBinary, string extAtContentDisposition, bool needSignature )
            : base( name, value, ParameterType.Post, needSignature )
        {
            this.IsBinary = isBinary;
            this.ExtAtContentDisposition = extAtContentDisposition;
        }

        public bool IsBinary
        {
            get;
            set;
        }

        public string ExtAtContentDisposition
        {
            get;
            set;
        }
    }
}
